package com.yylending.las.personal.feedback;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.Loader;
import android.text.*;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.yyfax.common.CLog;
import com.yyfax.common.tools.RegularExpress;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yylending.las.R;
import com.yylending.las.YJKD;
import com.yylending.las.base.BaseActivity;
import com.yylending.las.StatIDSet;
import com.yyfax.infrastructure.statistics.StatService;

public class FeedbackActivity extends BaseActivity implements FeedbackPresenter.View, TextWatcher, View.OnClickListener {
	private final static String TAG = "FeedbackActivity";

	private final static int LOADER_ID = 100;

	@BindView(R.id.feedback_content)
	EditText mContent;
	@BindView(R.id.feedback_content_length)
	TextView mLength;
	@BindView(R.id.feedback_submit)
	Button mFeedbackSubmit;

	private FeedbackPresenter mPresenter;

	public static void start(Activity aty) {
		Intent i = new Intent(aty, FeedbackActivity.class);
		aty.startActivity(i);
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return new PresenterLoader<>(this, new FeedbackPresenter.Generator());
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		if (null != data) {
			mPresenter = (FeedbackPresenter) data;
			mPresenter.attach(this);
			CLog.i(TAG, "FeedbackActivity onLoadFinished");
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_feedback);
		ButterKnife.bind(this);

		setTitle(R.string.title_activity_feedback);
		mContent.addTextChangedListener(this);
		getSupportLoaderManager().initLoader(LOADER_ID, null, this);
	}

	@Override
	public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
	}

	@Override
	public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
		int length = charSequence.length();
		SpannableString tx = new SpannableString(length + "/200");
		int splitIndex = tx.toString().indexOf("/");
		int color;
		if (length == 0) {
			color = ContextCompat.getColor(this, R.color.color_dbdbdb);
		} else if (length > 0 && length < 200) {
			color = ContextCompat.getColor(this, R.color.color_333333);
		} else {
			color = ContextCompat.getColor(this, R.color.color_ff0000);
		}
		tx.setSpan(new ForegroundColorSpan(color), 0, splitIndex + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		tx.setSpan(new ForegroundColorSpan(ContextCompat.getColor(this, R.color.color_dbdbdb)), splitIndex + 1, tx.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		mLength.setText(tx);
		if (length > 0) {		//输入内容为空时按钮不可点击
			mFeedbackSubmit.setEnabled(true);
		} else {
			mFeedbackSubmit.setEnabled(false);
		}
	}

	@Override
	public void afterTextChanged(Editable editable) {
	}

	@OnClick(R.id.feedback_submit)
	public void onViewClicked(View view) {
		switch (view.getId()) {
			case R.id.feedback_submit:
				StatService.onEvent(this, StatIDSet.EVENT_ID_FEEDBACK);
				YJKD.hideInputWindow(this);
				String content = mContent.getText().toString();
				if (TextUtils.isEmpty(content)) {
					showLongToast(R.string.feedback_content_hint);
				} else if (RegularExpress.existExpression(content)) {
					showLongToast(getString(R.string.contains_expression_hint, getString(R.string.question_and_suggestion)));
				} else {
					mPresenter.sendContent(content);
				}
				break;
		}
	}

	@Override
	public void feedbackSuccess() {
		showLongToast(getString(R.string.submit_loan_success));
		finish();
	}

	@Override
	public void feedbackFail(String msg) {
		showLongToast(msg);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		getSupportLoaderManager().destroyLoader(LOADER_ID);
	}
}
